﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

void mySwap(int a, int b)
{
	int tmp = a;
	a = b;
	b = tmp;

	cout << "mySwap::a =" << a << endl;//20
	cout << "mySwap::b =" << b << endl;//10
}
void mySwapAddr(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void mySwapRef(int& a, int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}
int& doWork()
{
	int a = 10;
	return a;
}
int& doWorkStatic()
{
	static int a = 10;
	return a;
}
void testParamDelivery()
{
	int a = 10;
	int b = 20;
	//1.值传递，不改变a,b的值；
	mySwap(a, b);

	cout << "a =" << a << endl;//10
	cout << "b =" << b << endl;//20

	//2.地址传递，会改变a,b的值；
	mySwapAddr(&a, &b);

	cout << "a =" << a << endl;//20
	cout << "b =" << b << endl;//10

	//3.引用传递，类似于地址传递；&a=a &b=b;
	mySwapRef(a, b);

	cout << "a =" << a << endl;//10
	cout << "b =" << b << endl;//20

	//4.引用的注意事项：
		//5.引用必须引一块合法的内存空间；
	//int& c = 10;
		//6.不要返回局部变量的引用；
	int& ret = doWork();
	//7.第一次ret=10，是编译器做了优化；后面的结果都不对，返回局部变量引用，是不对的。
	cout << "ret =" << ret << endl;//10
	cout << "ret =" << ret << endl;//2042153488
	cout << "ret =" << ret << endl;//2042153488


	int& retStatic = doWorkStatic();
	//8.如果函数的返回值是引用，那么这个函数调用可以作为左值；也就是可以作为变量；
	doWorkStatic() = 100;

	cout << "retStatic =" << retStatic << endl;//10 ->100
	cout << "retStatic =" << retStatic << endl;//10 ->100
	cout << "retStatic =" << retStatic << endl;//10 ->100
}

//main242：参数的传递方式
int main242()
{
	testParamDelivery();

	system("pause");
	return EXIT_SUCCESS;
}